package com.dycong.common.leetcode.ds.tree;

/**
 * @author dycong
 * @date 2019/12/4 8:35
 */
public class TreeHelper {

    private int index = 0;

    private int incrementAndGet() {
        return ++index;
    }

    public com.dycong.common.leetcode.ds.tree.TreeNode getTree(int max) {
        int i = 1;
        com.dycong.common.leetcode.ds.tree.TreeNode root = new com.dycong.common.leetcode.ds.tree.TreeNode(incrementAndGet());
        process(root, i, max);
        return root;
    }

    private void process(com.dycong.common.leetcode.ds.tree.TreeNode root, int start, int max) {
        if (null == root) return;
        if (start >= max) return;
        start++;
        com.dycong.common.leetcode.ds.tree.TreeNode left = new com.dycong.common.leetcode.ds.tree.TreeNode(incrementAndGet());
        com.dycong.common.leetcode.ds.tree.TreeNode right = new TreeNode(incrementAndGet());
        root.left = left;
        root.right = right;
        process(left, start, max);
        process(right, start, max);
    }

}
